/*
 * 功能：模块左边的导出
 *  编写日期: 2014-03-05
 *  作者:     David.Dai@zkteco.com 
 */
$("#model_export").click(function(){
		var app=$("#id_sys_cur_app").val();
	    var model=$("#id_sys_cur_model").val();
	    var dbapp_url=$("#id_sys_cur_appurl").val();
	    var reportname=$("#id_sys_cur_exporttitle").val();
	    var isModelExport=$("#id_sys_isModelExport").val();
        var exphtml=""
		
        $.ajax({
                type:"POST",
                dataType:"html",
                data:"reportname="+  reportname    ,           
                url:dbapp_url+model+"/export/show_export",
                success:function(data){
                        exphtml=data;
                        var div_export=$("<div id='id_export_dialog' title='"+gettext('导出')+"'>"+exphtml+"</div>");
                        $(document.body).append(div_export);
                        if (app=='list')
                             {
                                 div_export.find("#id_li_records").addClass("displayN");
                             }
			           			           
			           	div_export.find("#btnReturn").click(function(){
								$("#id_export_dialog").find("#id_close").click();            
			            });
			            div_export.find("#btnExport").click(function(){				            	
                            
                            $("#id_sys_cur_exporttitle").val($(".exportselect option:selected").html())
                            reportname=$("#id_sys_cur_exporttitle").val();
                            div_export.find("#exportform").append("<input type='hidden' value='"+reportname+"' name='reportname' \>");
			                div_export.find("#exportform").append("<input type='hidden' value='True' name='all' \>");
			                //导出方式
                            
                            div_export.find("input[name='t']").each(function(){
                                if($(this).attr("checked"))
                                {
                                    rt=$(this).val();
                                }
                            });
                            if(rt=="1")//全部导出
                            {
                                div_export.find("[name='all']").val("True");
                            }
                            if(rt=="2")//页导出
                            {
                                var l=div_export.find("#pagerecords").val();
                                if(!CheckNumber(l))
                                {
                                    alert(gettext('页记录数只能为数字'));
                                    div_export.find("#pagerecords").focus();
                                    return false;
                                }
                                var p1=div_export.find("#pagestart").val()
                                var p2=div_export.find("#pageend").val()
                                if(!CheckNumber(p1))
                                {
                                    alert(gettext('页码只能为数字'));
                                    div_export.find("#pagestart").focus();
                                    return false;
                                }
                                if(!CheckNumber(p2))
                                {
                                    alert(gettext('页码只能为数字'));
                                    div_export.find("#pageend").focus();
                                    return false;
                                }

                                div_export.find("#exportform").append("<input type='hidden' value='"+l+"' name='l' \>");
                                div_export.find("#exportform").append("<input type='hidden' value='"+p1+"' name='p1' \>");
                                div_export.find("#exportform").append("<input type='hidden' value='"+p2+"' name='p2' \>");
                            }
                            if(rt=="3")//记录导出
                            {
                            	div_export.find("[name='all']").val("False");
                                var s1=div_export.find("#recordstart").val()
                                var s2=div_export.find("#recordend").val()
                                if(!CheckNumber(s1))
                                {
                                    alert(gettext('记录数只能为数字'));
                                    div_export.find("#recordstart").focus();
                                    return false;
                                }
                                if(!CheckNumber(s2))
                                {
                                    alert(gettext('记录数只能为数字'));
                                    div_export.find("#recordend").focus();
                                    return false;
                                 
                                }
                                if(parseInt(s2)>10000)
                                {
                                     alert(gettext('记录条数不能超过10000'));
                                     div_export.find("#recordend").focus();
                                     return false;
                                     
                                }
                                if(parseInt(s2)<=0)
                                {
                                     alert(gettext('请输入大于0的数字'));
                                     div_export.find("#recordend").focus();
                                     return false;
                                     
                                }
                                if(parseInt(s1)<=0)
                               {
                                    alert(gettext('请输入大于0的数字'));
                                    div_export.find("#recordstart").focus();
                                    return false;
                                    
                               }
                                
                                div_export.find("#exportform").append("<input type='hidden' value='"+s1+"' name='s1' \>");
                                div_export.find("#exportform").append("<input type='hidden' value='"+s2+"' name='s2' \>");
                            }
							if(reportname!="")
							{
								$("#exportform").append("<input type='hidden' name='reportname' value='"+reportname+"'\>");
							}
                            var exportfields=$("#id_sys_cur_exportfields").val();
                            if(exportfields!="")
                            {
                            	$("#exportform").append("<input type='hidden' name='fields' value='"+exportfields+"'\>");
                            }
                            var format = div_export.find("[name='format']").val()
                            var all = div_export.find("[name='all']").val()
                            var recordstart = div_export.find("#recordstart").val()
                            var recordend = div_export.find("#recordend").val()
                            document.getElementById('grid_frame').contentWindow.do_new_export(format,reportname,recordstart,recordend,all)
                            return false		
			            });				            
			            div_export.dialog({title:gettext('导出')});
                }
        });

});
/*
 * 功能：检查是否是数字
 * 参数：number：传进来的值
 */
function CheckNumber(number)
{
    if(number!="")
    {
        var r,re;
        re = /\d*/i; //\d表示数字,*表示匹配多个数字
        r = number.match(re);
        if(r==number)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    };
};
